package cn.com.greatwall.system.modules.oauth2.rest;

import java.io.IOException;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.com.greatwall.logging.annotation.Log;
import cn.com.greatwall.system.modules.oauth2.domain.OauthClientDetails;
import cn.com.greatwall.system.modules.oauth2.service.OauthClientDetailsService;
import cn.com.greatwall.system.modules.oauth2.service.dto.criteria.OauthClientDetailsQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

/**
 * @Author herw
 * @Time 2020-12-14 10:00:05
 * @Version 1.0
 * @Description: TODO(用一句话描述该文件做什么)
 */
@Api(tags = "系统：客户端管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/client")
public class OauthClientDetailsController {
    private final OauthClientDetailsService oauthClientDetailsService;
    private static final String ENTITY_NAME = "client";
    
    @Log("导出客户端数据")
    @ApiOperation("导出客户端数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('client:list')")
    public void download(HttpServletResponse response, OauthClientDetailsQueryCriteria criteria) throws IOException {
        oauthClientDetailsService.download(oauthClientDetailsService.queryAll(criteria), response);
    }
    
    @Log("查询客户端")
    @ApiOperation("查询客户端")
    @GetMapping
    @PreAuthorize("@el.check('job:list','client:list')")
    public ResponseEntity<Object> query(OauthClientDetailsQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(oauthClientDetailsService.queryAll(criteria, pageable), HttpStatus.OK);
    }
    
    @Log("新增客户端")
    @ApiOperation("新增客户端")
    @PostMapping
    @PreAuthorize("@el.check('client:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody OauthClientDetails resources) {
        oauthClientDetailsService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }
    
    @Log("修改客户端")
    @ApiOperation("修改客户端")
    @PutMapping
    @PreAuthorize("@el.check('job:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody OauthClientDetails resources) {
        oauthClientDetailsService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除客户端")
    @ApiOperation("删除客户端")
    @DeleteMapping
    @PreAuthorize("@el.check('job:del')")
    public ResponseEntity<Object> delete(@RequestBody Set<String> ids) {
        oauthClientDetailsService.delete(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
